/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mekegi.whoanthro;

import java.util.Map;

import android.content.ContentValues;
import android.database.Cursor;

/**
 * @since 24.02.13 23:28
 * @author Arsen Abdusalamov <duburlan@gmail.com>
 */
abstract public class AbstractModel implements IModel {

      /** Value returned by {@link #getType(int)} if the specified column is null */
    static final int FIELD_TYPE_NULL = 0;

    /** Value returned by {@link #getType(int)} if the specified  column type is integer */
    static final int FIELD_TYPE_INTEGER = 1;

    /** Value returned by {@link #getType(int)} if the specified column type is float */
    static final int FIELD_TYPE_FLOAT = 2;

    /** Value returned by {@link #getType(int)} if the specified column type is string */
    static final int FIELD_TYPE_STRING = 3;

    /** Value returned by {@link #getType(int)} if the specified column type is blob */
    static final int FIELD_TYPE_BLOB = 4;

    protected Long id;
    public static final String KEY_ID="_id";

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id=id;
    }

    public AbstractModel() {
    }

    public AbstractModel(Long id) {
        this.id=id;
    }

    /**
     * @since 25.02.13 11:17
     * @author Arsen Abdusalamov <duburlan@gmail.com>
     * @param contentValues
     */
    public void fromContentValues(ContentValues contentValues) {
        if (contentValues.containsKey(KEY_ID)) {
            this.setId(contentValues.getAsLong(KEY_ID));
        }
    }

    public void fromCursor(Cursor cursor) {
        ContentValues contentValues=new ContentValues();
        for (Map.Entry<String, Integer> column: getColumnsMap().entrySet()) {
            String columnName=column.getKey();
            Integer columnIndex=cursor.getColumnIndex(columnName);
            switch (column.getValue()) {
                case FIELD_TYPE_INTEGER:
                    Long lVal=cursor.getLong(columnIndex);
                    if (lVal>Integer.MAX_VALUE||lVal<Integer.MIN_VALUE) {
                        contentValues.put(columnName, cursor.getInt(columnIndex));
                    } else {
                        contentValues.put(columnName, lVal);
                    }
                    break;
                case FIELD_TYPE_FLOAT:
                    Double dVal=cursor.getDouble(columnIndex);
                    if (dVal>Float.MAX_VALUE||dVal<Float.MIN_VALUE) {
                        contentValues.put(columnName, cursor.getFloat(columnIndex));
                    } else {
                        contentValues.put(columnName, dVal);
                    }
                    break;
                case FIELD_TYPE_STRING:
                    contentValues.put(columnName, cursor.getString(columnIndex));
                    break;
                case FIELD_TYPE_NULL:
                    //nothing
                    break;
            }
        }
        this.fromContentValues(contentValues);
    }

    /**
     * @since 25.02.13 11:17
     * @author Arsen Abdusalamov <duburlan@gmail.com>
     * @return ContentValues
     */
    public ContentValues toContentValues() {
        ContentValues result=new ContentValues();

        result.put(KEY_ID, this.getId());

        return result;
    }

    public static IModel factory(int modelName) {
        return ModelBuilder.factory(modelName);
    }

    abstract public String getTextForView();
}
